package com.cw.controller;

import com.cw.data.ConstantEnum;
import com.cw.data.Result;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class CommonController {

    @RequestMapping("/")
    public String login() {

        return "/login";
    }

    @RequestMapping("login")
    @ResponseBody
    public Result login(String oname, String pwd) {

        Subject subject = SecurityUtils.getSubject();

        UsernamePasswordToken token = new UsernamePasswordToken(oname, pwd);

        try {

            subject.login(token);

            return Result.result(ConstantEnum.SUCCESS).setData("登录成功!");

        }catch (UnknownAccountException ua) {

            return Result.result(ConstantEnum.ERROR).setData("用户名不存在");

        }catch (IncorrectCredentialsException ie) {

            return Result.result(ConstantEnum.ERROR).setData("密码错误!");
        }
    }
}
